//
//  CZEmoticonPageCell.swift
//  61.新工程表情键盘
//
//  Created by apple on 16/3/7.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

// 1.定义协议
protocol CZEmoticonPageCellDelegate: NSObjectProtocol {
    /// 按钮点击的代理方法
    func pageCellEmoticonButtonClick(button: CZEmoticonButton)
    
    // 删除按钮点击的代理方法
    func pageCellDeleteButtonClick()
}

class CZEmoticonPageCell: UICollectionViewCell {
    // MARK: - 属性
    // 2.定义代理属性
    var delegate: CZEmoticonPageCellDelegate?
    
    // 每页按钮列数
    let CZEmoticonColumnOfPage = 7
    
    // 每页行数
    let CZEmoticonRowOfPage = 3
    
    /// 每页显示的表情数组
    var pageEmoticons: [CZEmoticonModel]? {
        didSet {
            guard let emoticons = pageEmoticons else {
                print("emoticons 为空")
                return
            }
            
            // 隐藏所有按钮,来防止cell重复利用
            for button in emoticonButtons {
                button.hidden = true
            }
            
            print("我有\(emoticons.count)个表情模型")
            // 将表情模型显示在对应的按钮上面
            for i in 0..<emoticons.count {
                
                // 获取每一个表情模型
                let emoticon = emoticons[i]
                
                // 获取对应的按钮,按钮要显示内容,将按钮显示出来
                let emoticonButton = emoticonButtons[i]
                emoticonButton.hidden = false
                emoticonButton.emoticon = emoticon
            }
        }
    }
    
    /// 20个按钮的数组
    private var emoticonButtons: [CZEmoticonButton] = [CZEmoticonButton]()
    
    var indexPath: NSIndexPath? {
        didSet {
//            debugLabel.text = "当前显示第\(indexPath!.section)组,第\(indexPath!.item)个cell"
            
            // indexPath.section != 0 就隐藏recentLabel
            recentLabel.hidden = indexPath?.section != 0
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
        // 添加子控件
        addEmoticonButtons()
        contentView.addSubview(debugLabel)
        contentView.addSubview(recentLabel)
        
        backgroundColor = UIColor.clearColor()
        // 设置大小
        debugLabel.frame = CGRect(origin: CGPointZero, size: CGSize(width: 300, height: 150))
    }
    
    /**
     添加20个表情按钮
     */
    private func addEmoticonButtons() {
        // 在init里面cell还没有frame (0, 0, 0, 0)
        for _ in 0..<CZEmoticonNumberOfPage {
            let emoticonButton = CZEmoticonButton(type: UIButtonType.Custom)
            
            // 设置按钮的title让emoji变大
            emoticonButton.titleLabel?.font = UIFont.systemFontOfSize(32)
//            emoticonButton.backgroundColor = UIColor.randomColor()
            
            emoticonButton.addTarget(self, action: "emoticonButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
            // 添加contentView
            contentView.addSubview(emoticonButton)
            
            // 将按钮再添加到数组里面方便下次直接遍历按钮
            emoticonButtons.append(emoticonButton)
        }
    }
    
    // MARK: - 按钮点击事件
    @objc private func emoticonButtonClick(button: CZEmoticonButton) {
        // 3.调用代理
        delegate?.pageCellEmoticonButtonClick(button)
    }
    
    @objc private func deleteButtonClick() {
        delegate?.pageCellDeleteButtonClick()
    }
    
    /**
     布局表情按钮的位置
     */
    private func layoutEmoticonButtons() {
        // 左右的间距
        let marginLR: CGFloat = 5
        
        // 底部间距
        let marginBottom: CGFloat = 25
        
        // 宽高都一样,只需要计算一次
        // 一个按钮的宽度 = (cell的宽度 - 左右间距) / 列数
        let itemW = (frame.width - 2 * marginLR) / CGFloat(CZEmoticonColumnOfPage)
        
        // 一个按钮的高度 = (cell的高度 - 底部间距) / 行数
        let itemH = (frame.height - marginBottom) / CGFloat(CZEmoticonRowOfPage)
        
        // 遍历按钮,设置frame
        for i in 0..<emoticonButtons.count {
            // 计算按钮在哪行哪列
            let column = i % CZEmoticonColumnOfPage
            let row = i / CZEmoticonColumnOfPage
            
            // 计算x值
            let x = marginLR + CGFloat(column) * itemW
            
            // 计算y值
            let y = CGFloat(row) * itemH
            
            // 设置按钮的frame
            emoticonButtons[i].frame = CGRect(x: x, y: y, width: itemW, height: itemH)
        }
        
        // 设置删除按钮的位置
        // 计算删除x值
        let deleteX = marginLR + CGFloat(CZEmoticonColumnOfPage - 1) * itemW
        
        // 计算删除y值
        let deleteY = CGFloat(CZEmoticonRowOfPage - 1) * itemH
        deleteButton.frame = CGRect(x: deleteX, y: deleteY, width: itemW, height: itemH)
        
        // 设置label的位置
        recentLabel.center = CGPoint(x: frame.width * 0.5, y: frame.height - recentLabel.bounds.height)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        layoutEmoticonButtons()
    }
    
    // MARK: - 懒加载
    /// 调试的lable
    private lazy var debugLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(30)

        return label
    }()
    
    /// 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        self.contentView.addSubview(button)
        
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 最近使用的表情label
    private lazy var recentLabel: UILabel = {
        let label = UILabel()
        
        label.text = "最近使用的表情"
        label.font = UIFont.systemFontOfSize(12)
        label.textColor = UIColor.lightGrayColor()
        
        label.sizeToFit()
        
        return label
    }()
}
